;
$(function(){
	 var basic_arr,team_arr,course_arr;  		
    // $('.top_bar a').click(function(){
    // 	$(this).addClass('active').siblings('a').removeClass('active');
    // 	$('.tab_panel').eq($(this).index()).show().siblings().hide();
    // });
    // icon划过浮层tips
   $(document).delegate('[icontip]','mouseenter mouseleave',function(data){
       data = $(this).attr('icontip');                
       $('body').append('<span class="icontip bottom">'+data+'</span>');
       var iHeight = $('.icontip').innerHeight();
       var iWidth = $('.icontip').innerWidth();
       $('.icontip').css({
           top:$(this).offset().top-iHeight,
           left:$(this).offset().left-iWidth/2+10,
       });
   });
   $(document).delegate('[icontip]','mouseleave',function(data){
       $('.icontip').remove();
   });	
	
	$('.team_list').delegate('.edit_person','click',function(){
		var _o = $(this).parents('.show_mow');
		var _i = $(this).parents('.show_mow').siblings('.edit_mow');
		var person = { 
			"name": "", 
			"job": "" ,
			"tel":"",
			"intro":""
		};
		person.name = _o.find('.person span').html();
		person.job = _o.find('.person b').html();
		person.tel = _o.find('.tel b').html();
		person.intro = _o.find('.team_txt').html();
		_i.find('input[name="name"]').val(person.name);
		_i.find('input[name="job"]').val(person.job);
		_i.find('input[name="tel"]').val(person.tel);
		_i.find('textarea[name="intro"]').text(person.intro);
		$(this).parents('.show_mow').hide().siblings('.edit_mow').show();
	});
	$('.team_list').delegate('.save_person','click',function(){
		var _o = $(this).parents('.edit_mow');
		var _i = $(this).parents('.edit_mow').siblings('.show_mow');
		var person = { 
			"name": "", 
			"job": "" ,
			"tel":"",
			"intro":""
		};
		person.name = _o.find('input[name="name"]').val();
		person.job = _o.find('input[name="job"]').val();
		person.tel = _o.find('input[name="tel"]').val();
		person.intro = _o.find('textarea[name="intro"]').val();
		if(person.name == '' || person.name == null){
			alert('请填写姓名');
			return false;
		}
		if(person.job == '' || person.job == null){
			alert('请填写职位');
			return false;
		}
		if(person.tel == '' || person.tel == null){
			alert('请填写电话');			
			return false;
		}
		if(person.tel != '' || person.tel != null){
			if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(person.tel))){
				alert('请填写正确的格式');	
				return false;		
			}
		}
		if(person.intro == '' || person.intro == null){
			alert('请填写介绍');
			return false;
		}
		_i.find('.person span').html(person.name);
		_i.find('.person b').html(person.job);
		_i.find('.tel b').html(person.tel);
		_i.find('.team_txt').html(person.intro);
		$(this).parents('.edit_mow').hide().siblings('.show_mow').show();
	});
	$('.add_person').click(function(){
		$(this).siblings('.add_mow').show();	
	});	
	$('.add_mow').delegate('.add_save_person','click',function(){
		var _o = $(this).parents('.add_mow');
		var person = { 
			"name": "", 
			"job": "" ,
			"tel":"",
			"intro":""
		};
		person.name = _o.find('input[name="name"]').val();
		person.job = _o.find('input[name="job"]').val();
		person.tel = _o.find('input[name="tel"]').val();
		person.intro = _o.find('textarea[name="intro"]').val();
		if(person.name == '' || person.name == null){
			alert('请填写姓名');
			return false;
		}
		if(person.job == '' || person.job == null){
			alert('请填写职位');
			return false;
		}
		if(person.tel == '' || person.tel == null){
			alert('请填写电话');			
			return false;
		}
		if(person.tel != '' || person.tel != null){
			if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(person.tel))){
				alert('请填写正确的格式');	
				return false;		
			}
		}
		if(person.intro == '' || person.intro == null){
			alert('请填写介绍');
			return false;
		}
		var html = '<div class="team_row"><div class="show_mow"><div class="top_line clearfix"><div class="person"><span>'+person.name+'</span><b>'+person.job+'</b></div><div class="tel"><span>联系电话：</span><b>'+person.tel+'</b></div><div class="teamBtn"><i class="iconfont icon-bianji1 edit_person" icontip="编辑"></i><i class="iconfont icon-shanchu2" icontip="删除"></i></div></div><p class="team_txt">'+person.intro+'</p></div><div class="edit_mow"><div class="input_line"><div class="pull-left"><input type="text" placeholder="姓名" name="name"><input type="text" placeholder="职位" name="job"><input type="text" placeholder="手机号码" name="tel"></div><div class="pull-right"><i class="iconfont icon-msnui-save-bold save_person" icontip="保存"></i></div></div><div class="edit_moreTxt"><textarea placeholder="包括教育经历、工作经验、创业经历、行业背景等内容" name="intro"></textarea></div></div></div>';
		_o.find('input[name="name"]').val('');
		_o.find('input[name="job"]').val('');
		_o.find('input[name="tel"]').val('');
		_o.find('textarea[name="intro"]').val('');
		_o.hide();
		$('.team_list').append(html);
	});	
	function addDataPicker(ele){
		$(ele).delegate('input[name="time"]','focus',function(){
	    	$(this).daterangepicker({
		        singleDatePicker: true,
		        showDropdowns: true,
		        maxDate:new Date(),
		        //本地化配置
			    locale : {  
			    	format: "YYYY-MM-DD",
			        applyLabel : '确定',  
			        cancelLabel : '取消',  
			        fromLabel : '起始时间',  
			        toLabel : '结束时间',  
			        customRangeLabel : '自定义',  
			        daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],  
			        monthNames : [ '1月', '2月', '3月', '4月', '5月', '6月',  
			                '7月', '8月', '9月', '10月', '11月', '12月' ],  
			        firstDay : 1  
			    }  
		    }, 
		    function(start, end, label) {
		        
		    });
	    })
	}  
	addDataPicker('.add_mow');
	addDataPicker('.course_list');    
	$('.course_list').delegate('.edit_course','click',function(){
		var _o = $(this).parents('.show_mow');
		var _i = $(this).parents('.show_mow').siblings('.edit_mow');
		var course = { 
			"time": "",
			"intro":""
		};
		course.time = _o.find('.time span').html();
		course.intro = _o.find('.course_txt').html();
		_i.find('input[name="time"]').val(course.time);
		_i.find('textarea[name="intro"]').text(course.intro);
		$(this).parents('.show_mow').hide().siblings('.edit_mow').show();
	});
	$('.course_list').delegate('.save_course','click',function(){
		var _o = $(this).parents('.edit_mow');
		var _i = $(this).parents('.edit_mow').siblings('.show_mow');
		var course = { 
			"time": "",
			"intro":""
		};
		course.time = _o.find('input[name="time"]').val();
		course.intro = _o.find('textarea[name="intro"]').val();
		if(course.time == '' || course.time == null){
			alert('请选择日期');
			return false;
		}
		if(course.intro == '' || course.intro == null){
			alert('请填写介绍');
			return false;
		}
		_i.find('.time span').html(course.time);
		_i.find('.course_txt').html(course.intro);
		$(this).parents('.edit_mow').hide().siblings('.show_mow').show();
	});
	$('.add_course').click(function(){
		$(this).siblings('.add_mow').show();	
	});	
	$('.add_mow').delegate('.add_save_course','click',function(){
		var _o = $(this).parents('.add_mow');
		var course = { 
			"time": "", 
			"intro":""
		};
		course.time = _o.find('input[name="time"]').val();
		course.intro = _o.find('textarea[name="intro"]').val();
		if(course.time == '' || course.time == null){
			alert('请选择日期');
			return false;
		}
		if(course.intro == '' || course.intro == null){
			alert('请填写介绍');
			return false;
		}
		var html = '<div class="course_row"><div class="show_mow"><div class="top_line clearfix"><div class="time"><i class="iconfont icon-rili"></i> <span>'+course.time+'</span></div><div class="courseBtn"><i class="iconfont icon-bianji1 edit_course" icontip="编辑"></i> <i class="iconfont icon-shanchu2" icontip="删除"></i></div></div><p class="course_txt">'+course.intro+'</p></div><div class="edit_mow"><div class="input_line"><div class="pull-left"><div class="time_input"><input type="text" placeholder="日期" name="time" readonly> <i class="iconfont icon-rili" icontip="日期"></i></div></div><div class="pull-right"><i class="iconfont icon-msnui-save-bold save_course" icontip="保存"></i></div></div><div class="edit_moreTxt"><textarea placeholder="项目历程介绍" name="intro"></textarea></div></div></div>';
		_o.find('input[name="time"]').val('');
		_o.find('textarea[name="intro"]').val('');
		_o.hide();
		$('.course_list').append(html);
	});	
	// 上下步
	$('.first_next,.three_prev').click(function(){
		$('.tab_panel').eq(1).show().siblings().hide();
		$('.top_bar a').eq(1).addClass('active').siblings().removeClass('active');
	});
	$('.two_next').click(function(){
		$('.tab_panel').eq(2).show().siblings().hide();
		$('.top_bar a').eq(2).addClass('active').siblings().removeClass('active');
	});
	$('.two_prev').click(function(){
		$('.tab_panel').eq(0).show().siblings().hide();
		$('.top_bar a').eq(0).addClass('active').siblings().removeClass('active');
	});
	/*------------------------------------------------------
	 * Util
	 ------------------------------------------------------*/
	
	//项目简介输入时提示字数限制
	$("#project_comment").on("input",function(){
		var len=$(this).val().length;
		
		$(this).siblings(".txt-tip").html(len + "/500");
	});
	
	//错误提示函数
	function errTips(obj,str){
		var exist=obj.next().hasClass("err");
		
		if(exist){
			obj.next().html(str);
		}else{
			var $err=$("<div class='err text-left'></div");
			
			$err.html(str);
			obj.after($err);
		}
		
		$(".err").show();
	};
	
	
	/*------------------------------------------------------------
	 * 下拉选择框展开
	 -----------------------------------------------------------*/
	//1、点击选框时，展开、收起下拉选项
	$(".select").on("click",function(e){
		e.stopPropagation();
		
		var $tag,hidden;
		
		$tag=$(this).find(".options-box");
		hidden=$tag.css("display")=== "none" ? true : false;
		
		//显示展开选项
		if(hidden){
			$(this).addClass("now").siblings().removeClass("now");
			$(".options-box").slideUp();
			$tag.slideDown();
		}
		
	});
	
	//点击具体选项时收起下拉框
	$(".options-box .options").click(function(e){
		var value,show_val;
		
		e.stopPropagation();
		value=$(this).data('id');
		show_val=$(this).text();
		$(this).addClass("selected").siblings().removeClass("selected");
		
		//赋值
		$(this).parents(".select").find("input:first").val(show_val).attr('data-val',value);

		//收起选项
		$(this).parents(".options-box").slideUp();
		
	});
	
	//点击下拉框外区域收起下拉选项
	$(document).click(function(){
		$(".options-box").slideUp();
	});
	
	
	/*----------------------------------------------------------
	 * 标签
	 ---------------------------------------------------------*/
	//点击亮点标签输入框时，input获取焦点，描边变亮
	$(".tags").click(function(e){
		e.stopPropagation();
		$(this).addClass("focus");
		$("#tag").focus();
	});
	
	//点击文档其他区域时
	$(document).click(function(){
		addTag();
	});
	
	//按下enter键时
	$(document).on("keydown",function(e){
        if(e && e.keyCode==13){ // enter 键
            addTag();
        }
	});
	$('.tags').delegate('i.del','click',function(){
		$(this).parent("span").remove();
	})
	
	//添加标签函数
	function addTag(){
		var value=$("#tag").val();
		
		//如果输入框不为空，添加标签
		if(!(value=="" || value==null)){
			var arr = $('.tags span');
			var flag = true;
			$.each(arr,function(i){
				if(arr.eq(i).text() ==value){
					flag = false;
					return flag;
				}
			});
			if(flag){				
				var $span=$("<span></span>"),
					str=value + "<i class='del iconfont icon-shanchu'></i>";					
				$span.html(str);
				$("#tag").before($span);		
			}
		}		
		$(".tags").removeClass("focus");
		$("#tag").val("");
	};
	
	/**显示上传进度条**/
	$("#book").click(function(){
		$(".pmgressbar-box").fadeIn();
	});
	
	//取消上传时，隐藏上传进度条
	$(".pmgressbar-cancel").click(function(){
		$(".pmgressbar-box").fadeOut();
	});
	
	
	/*----------------------------------------------------------
	 *表单提交
	 ----------------------------------------------------------*/	
	$("#memberform").validate({
		debug:true,
	    rules: {
	      file:{
	      	required:true
	      },
	      project_name: {
	        required: true,
	        maxlength: 15
	      },	
	      business:{
	      	required: true,
	      },
	      round:{
	      	required: true,
	      },
	      city:{
	      	required: true,
	      },
	      link:{
	      	required: false,
	      	url:true
	      	// isLink:true
	      },
	      book:{
	      	required: true,
	      },
	      equity:{
	      	required: true,
	      	// digits:true
	      },
	      money:{
	      	required: true,
	      	// digits:true,
	      	maxlength: 10
	      },
	      currency:{
	      	required: true,
	      },
	      contact_name:{
	      	required: true,
	      },
	      contact_phone:{
	      	required: true,
	      	isPhone:true
	      },
	      project_comment:{
	      	required: true,
	      }      
	    },
	    messages: {
	       file:{
	      	 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请上传项目logo",
	       },
	       project_name: {
	         required: "<i class='iconfont icon-wenti1'></i>&nbsp;请输入您的项目名称",
	         maxlength: "<i class='iconfont icon-wenti1'></i>真实姓名须为8字以内"
	       },
	       business:{
	      	 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请选择一个行业",
	       },
	       round:{
			 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请选择一个轮次",
	       },
	       city:{
			 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请选择一个地区",
	       },
	       link:{
	       	 url: "<i class='iconfont icon-wenti1'></i>&nbsp;请正确填写您的产品链接",
			 // isLink: "<i class='iconfont icon-wenti1'></i>&nbsp;请正确填写您的产品链接",
	       },
	       book:{
			 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请上传商业计划书",
	       },
	      equity:{
			 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请填写出让股权",
             // digits: "<i class='iconfont icon-wenti1'></i>&nbsp;融资额度须为整数",
	       },
	       money:{
			 required: "<i class='iconfont icon-wenti1'></i>&nbsp;融资额度不能为空",
             // digits: "<i class='iconfont icon-wenti1'></i>&nbsp;融资额度须为整数",
             maxlength: "<i class='iconfont icon-wenti1'></i>&nbsp;融资额度应不超过10位数"
	       },
	       currency:{
	       	 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请选择币种",
	       },
	       contact_name:{
	       	 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请填写联系人姓名",
	       },
	       contact_phone:{
	       	 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请填写联系人手机",
	       	 isPhone:"<i class='iconfont icon-wenti1'></i>&nbsp;请填写正确的手机号码"
	       },
	       project_comment:{
	       	 required: "<i class='iconfont icon-wenti1'></i>&nbsp;请填写项目简介",
	       }
	    },
	    submitHandler:function(form){
	    	var tags = "";
	    	var $tag = $('.tags span');
	    	
	    	$.each($tag,function(i){
	    		//tags.push($tag.eq(i).text());
	    		tags+=$tag.eq(i).text()+";";
	    	});
	    	var unit = 1;
	    	var currency = $('#currency').attr('data-val');
	    	if(currency=='人民币') {
	    		unit = 1;
	    	} else {
	    		unit = 2;
	    	}
	    	var basic = {
	    		'logo':$('#logoButton').attr("src"),
	    		'name':$('#project_name').val().trim(),
	    		'fields':$('#business').attr('data-val'),
	    		'round':$('#round').attr('data-val'),
	    		'area':$('#city').attr('data-val'),
	    		'link':$('#link').val(),
	    		'bp':$('#BpButton').attr("src"),
	    		'stock':$('#equity').val(),
	    		'money':$('#money').val(),
	    		'unit':unit,
	    		'lables':tags,
	    		'contactsName':$('#contact_name').val().trim(),
	    		'contactsPhone':$('#contact_phone').val(),
	    		'proDetail':$('#project_comment').val().trim(),
	    		'id':$('#proDeliveryId').val(),
	    	};
	    	basic_arr = basic;
	    	$('.tab_panel').eq(1).show().siblings().hide();
		    $('.top_bar a').eq(1).addClass('active').siblings().removeClass('active');
		    console.log($('#business').attr('data-val'));
	    }
	});
	// 自定义添加
	// jQuery.validator.addMethod("isLink", function(value, element) {
	//     var re=new RegExp("^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$", "gi");
	//     return this.optional(element) || (re.test(value));
	// }, "请正确填写您的产品链接");
	/*jQuery.validator.addMethod("isPercent", function(value, element) {
	    var patt1 = new RegExp(/^\d+%$/);
	    return this.optional(element) || (patt1.test(value));
	}, "请填写正确的数字");*/
	jQuery.validator.addMethod("isPhone", function(value, element) {
        var phone = new RegExp(/^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/);
        return this.optional(element) || (phone.test(value));
    }, "请填写正确的手机号码");

	// 投递项目
	$('#deliver_pro').click(function(){
		// 团队信息
		var teamList = $('.team_list .team_row');	
		team_arr = [];		
		$.each(teamList,function(i){
			var arr = {
		        'id':teamList.eq(i).find('.person').data('id'),
		        'proId':teamList.eq(i).find('.person').data('name'),
		        'name':teamList.eq(i).find('.person span').html(),
	    		'position':teamList.eq(i).find('.person b').html(),
	    		'phone':teamList.eq(i).find('.tel b').html(),
	    		'detail':teamList.eq(i).find('.team_txt').html(),
		    }
		    team_arr.push(arr);
		});
		// 项目历程
		var courseList = $('.course_list .course_row');	 
		course_arr = [];		
		$.each(courseList,function(i){
			var arr = {
				'id':courseList.eq(i).find('.time').data('id'),
				'proId':courseList.eq(i).find('.time').data('name'),
		        'createTime':courseList.eq(i).find('.time span').html(),
	    		'detail':courseList.eq(i).find('.course_txt').html(),
		    }
		    course_arr.push(arr);
		});
		var deliveryId = $("#deliveryId").val();  //接收投递项目用户id
		$.ajax({  
    	    type: "POST",  
    	    url: "/entrepren/create",  
    	    dataType: 'json',
    	    //contentType : 'application/json;charset=utf-8', //设置请求头信息
    	    data: {"proDeliveryJson":JSON.stringify(basic_arr),"organizationJson" : JSON.stringify(team_arr), "milepostJson" : JSON.stringify(course_arr),"deliveryId":deliveryId},  
    	    success: function(data){  
    	    },  
    	    error: function(res){  
    	    }  
    	}); 
		 console.log(course_arr);//项目历程数组
		 console.log(team_arr);//团队信息数组
		 console.log(basic_arr);//基本信息数据
	});
	
	
});
function delOrganization(id){
	$.ajax({  
		type: "POST",  
		url: "/entrepren/deleteOrganization",  
		dataType: 'json',
		data: {"id":id},  
		success: function(data){
			alert(data.data);
		},  
		error: function(res){  
		}  
	}); 
}
function delMilepost(id){
	$.ajax({  
		type: "POST",  
		url: "/entrepren/deleteMilepost",  
		dataType: 'json',
		data: {"id":id},  
		success: function(data){
			alert(data.data);
		},  
		error: function(res){  
		}  
	}); 
}